﻿using CL.Business.Entity;
using CL.Dapper.Repository;
using System.Data;
using CL.Enum.Common;
using System.Collections.Generic;
using System.Linq;
using CL.View.Entity.Business;
using Dapper;
using System.Text;

namespace CL.Business.DAL
{
    public class AccountDAL : DataRepositoryBase<AccountEntity>
    {
        public AccountDAL(DbConnectionEnum conenum, IDbConnection Db = null) : base(conenum, Db)
        {
        }
        /// <summary>
        /// 根据用户名登录
        /// </summary>
        /// <param name="UnionName"></param>
        /// <returns></returns>
        public AccountEntity QueryEntity(string AccountCode)
        {
            return base.Get(new { AccountCode = AccountCode }, " AccountID desc ");
        }

        /// <summary>
        /// 查询用户对象
        /// </summary>
        /// <param name="AccountID"></param>
        /// <returns></returns>
        public AccountEntity QueryEntity(int AccountID)
        {
            return base.Get(AccountID);
        }

        /// <summary>
        /// 根据身份证查询用户对象
        /// </summary>
        /// <param name="AccountID"></param>
        /// <returns></returns>
        public AccountEntity QueryEntityByIdCard(string IdentityID)
        {
            return base.Get(new { IdentityID = IdentityID }, " AccountID desc ");
        }

        public AccountEntity QueryEntityByMobile(string phoneNum)
        {
            return base.Get(new { UserMobile = phoneNum }, " UserMobile desc ");
        }
        /// <summary>
        /// 根据银行卡查询用户对象
        /// </summary>
        /// <param name="AccountID"></param>
        /// <returns></returns>
        public AccountEntity QueryEntityByBankID(string bankId)
        {
            return base.Get(new { BankCard = bankId }, " AccountID desc ");
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="AccountID"></param>
        /// <param name="AccountPwd"></param>
        /// <returns></returns>
        public bool ModifyEntity(int AccountID, string AccountPwd)
        {
            using (IDbTransaction tran = base.db.BeginTransaction())
            {
                try
                {
                    AccountEntity Entity = base.Get(AccountID, tran);
                    Entity.AccountPwd = AccountPwd;
                    base.Update(Entity, tran);
                    tran.Commit();
                    return true;
                }
                catch
                {
                    tran.Rollback();
                    return false;
                }
            }
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int UpdateEntity(AccountEntity Entity)
        {
            return base.Update(Entity);
        }




        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int InsertEntity(AccountEntity Entity)
        {
            return base.Insert(Entity) ?? 0;
        }


        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <returns></returns>
        public List<AccountEntity> QueryListByPage()
        {                      
            return base.GetList(new { AccountStatus = 0 }, "AccountID asc").ToList();
        }



        public List<AccountEntity> QueryListByPage(string orderby,int pageIndex, int pageSize, string searchKey, ref int recordCount)
        {
            if(string.IsNullOrEmpty(searchKey))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" 1 = 1");
                recordCount = base.GetIntSingle(" SELECT  count(1) FROM dbo.UN_Account");
                return base.GetListPaged(pageIndex, pageSize, strSql.ToString(), orderby).ToList();
            }
            else
            {
                searchKey = "%" + searchKey + "%";
                var para = new DynamicParameters();
                para.Add("@AccountCode", searchKey);
                para.Add("@UserMobile", searchKey);
                para.Add("@FullName", searchKey);
                StringBuilder strSql = new StringBuilder();
                string sqlWhere = "(AccountCode LIKE @AccountCode OR UserMobile LIKE @UserMobile OR FullName LIKE @FullName)";
                strSql.Append(sqlWhere);
                recordCount = base.GetIntSingle(" SELECT  count(1) FROM dbo.UN_Account WHERE " + sqlWhere, para);
                return base.GetListPaged(pageIndex, pageSize, strSql.ToString(), orderby, para).ToList();
            }
          
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelEntity(int id)
        {
            return base.Delete(id) > 0;
        }



        public List<udv_Account> QueryAccountList(string AccountCode)
        {
            var para = new DynamicParameters();
            para.Add("@AccountCode", AccountCode);
            return new DataRepositoryBase<udv_Account>(DbConnectionEnum.CaileBusiness).QueryList("udp_QueryAgentList", para, CommandType.StoredProcedure).ToList();
        }       
    }
}
